import { createContext, useContext, useState } from 'react';

const UserContext = createContext();

export const useUserContext = () => useContext(UserContext);

export const UserProvider=({ children }) =>{
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  const [user, setUser] = useState(null);
  const [routeName, setRouteName] = useState(null);

  const configDefault={
    //路由设置
    routesName:'',
    //TabBar默认配置
    tabDefault:'home',
    // 基础样式
    styles: {
      '--adm-tab-bar-height': '50px',
      '--adm-tab-bar-item-color':'grey',
      '--adm-tab-bar-item-active-color': 'var(--adm-color-primary)',
    },
  }

  const recordRouteName = (name) => {
    setRouteName(name);
  }

  const getRouteName = () => {
    return routeName;
  }

  const login = (userData) => {
    setUser(userData);
    setIsAuthenticated(true);
  };

  const logout = () => {
    setUser(null);
    setIsAuthenticated(false);
  };

  return (
    <UserContext.Provider value={{ isAuthenticated, user, login, logout, configDefault,recordRouteName, getRouteName }}>
      {children}
    </UserContext.Provider>
  );
}
